<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据平台</el-breadcrumb-item>
      <el-breadcrumb-item>任务开发</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索添加 -->
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @click="getTableMap">
            <el-button slot="append" icon="el-icon-search" @click="getTableMap"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true ">添加</el-button>
          <el-button type="primary">开发</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableMap" border stripe>
        <!-- <el-table-column label="选择" width="45px">
          <template>
            <el-checkbox v-model="checked"></el-checkbox>
          </template>
        </el-table-column>-->

        <el-table-column type="index"></el-table-column>
        <el-table-column label="批次" prop="batch_no" width="60px"></el-table-column>
        <el-table-column label="来源系统" prop="from_system_id"></el-table-column>
        <el-table-column label="来源Schema" prop="from_schema"></el-table-column>
        <el-table-column label="来源表" prop="from_table"></el-table-column>
        <el-table-column label="目标系统" prop="to_system_id"></el-table-column>
        <el-table-column label="目标Schema" prop="to_schema"></el-table-column>
        <el-table-column label="目标表" prop="to_table"></el-table-column>
        <el-table-column label="开发 模板" prop="template_no" width="60px"></el-table-column>
        <!-- <el-table-column label="创建时间" prop="create_date"></el-table-column>
        <el-table-column label="修改时间" prop="update_date"></el-table-column>-->
        <el-table-column label="开发 标识" prop="dev_flag" width="60px"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              content="修改"
              @click="showUpdateDialog(scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              content="删除"
              @click="deleteTableMap(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 创建新用户对话框 -->
    <el-dialog title="添加" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="105px">
        <!-- 用户名 -->
        <el-form-item label="来源系统" prop="from_system_id">
          <el-input v-model="addForm.from_system_id"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="来源Schema" prop="from_schema">
          <el-input v-model="addForm.from_schema"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="来源表" prop="from_table">
          <el-input v-model="addForm.from_table"></el-input>
        </el-form-item>
        <el-form-item label="目标系统" prop="to_system_id">
          <el-input v-model="addForm.to_system_id"></el-input>
        </el-form-item>
        <el-form-item label="目标Schema" prop="to_schema">
          <el-input v-model="addForm.to_schema"></el-input>
        </el-form-item>
        <el-form-item label="目标表" prop="to_table">
          <el-input v-model="addForm.to_table"></el-input>
        </el-form-item>
        <el-form-item label="开发模板" prop="template_no">
          <el-input v-model="addForm.template_no"></el-input>
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="create_date">
          <el-input v-model="addForm.create_date"></el-input>
        </el-form-item>
        <el-form-item label="修改时间" prop="update_date">
          <el-input v-model="addForm.update_date"></el-input>
        </el-form-item>-->
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTableMap">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户信息 -->
    <el-dialog
      title="修改"
      :visible.sync="updateDialogVisible"
      width="50%"
      @close="updateDialogClose"
    >
      <!-- 内容主体区域 -->
      <el-form :model="updateForm" :rules="updateFormRules" ref="updateFormRef" label-width="105px">
        <el-form-item label="来源系统" prop="from_system_id">
          <el-input v-model="updateForm.from_system_id"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="来源Schema" prop="from_schema">
          <el-input v-model="updateForm.from_schema"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="来源表" prop="from_table">
          <el-input v-model="updateForm.from_table"></el-input>
        </el-form-item>
        <el-form-item label="目标系统" prop="to_system_id">
          <el-input v-model="updateForm.to_system_id"></el-input>
        </el-form-item>
        <el-form-item label="目标Schema" prop="to_schema">
          <el-input v-model="updateForm.to_schema"></el-input>
        </el-form-item>
        <el-form-item label="目标表" prop="to_table">
          <el-input v-model="updateForm.to_table"></el-input>
        </el-form-item>
        <el-form-item label="开发模板" prop="template_no">
          <el-input v-model="updateForm.template_no"></el-input>
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="create_date">
          <el-input v-model="addForm.create_date"></el-input>
        </el-form-item>
        <el-form-item label="修改时间" prop="update_date">
          <el-input v-model="addForm.update_date"></el-input>
        </el-form-item>-->
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateTableMap">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.getTableMap();
  },
  data() {
    return {
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5,
      },
      tableMap: [],
      total: 0,
      addDialogVisible: false, //对话框显示状态
      //新增表单信息
      addForm: {
        from_schema: "",
        from_system_id: "",
        from_table: "",
        to_schema: "",
        to_system_id: "",
        to_table: "",
        db_type: "",
        create_date: "",
        update_date: "",
        dev_flag: "",
      },
      //修改表单信息
      updateForm: {},
      // 显示/隐藏修改用户栏
      updateDialogVisible: false,
      updateFormRules: {
        from_system_id: [
          { required: true, message: "请输入来源系统", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        from_schema: [
          { required: true, message: "请输入来源Schema", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        from_table: [
          { required: true, message: "请输入来源表", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        to_system_id: [
          { required: true, message: "请输入目标系统", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        to_schema: [
          { required: true, message: "请输入目标Schema", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        to_table: [
          { required: true, message: "请输入目标表", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      addFormRules: {
        from_system_id: [
          { required: true, message: "请输入来源系统", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        from_schema: [
          { required: true, message: "请输入来源Schema", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        from_table: [
          { required: true, message: "请输入来源表", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        to_system_id: [
          { required: true, message: "请输入目标系统", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        to_schema: [
          { required: true, message: "请输入目标Schema", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        to_table: [
          { required: true, message: "请输入目标表", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async getTableMap() {
      const { data: res } = await this.$http.get("tablemap", {
        params: this.queryInfo,
      });
      this.tableMap = res.data;
      this.total = res.num;
    },
    // 最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getTableMap();
    },
    // pageNum触发动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getTableMap();
    },
    // 监听添加用户操作
    addDialogClose() {
      this.$refs.addFormRef.resetFields();
    },
    // 添加用户
    addTableMap() {
      this.$refs.addFormRef.validate(async (valid) => {
        console.log(this.addForm);
        if (!valid) return;
        // 发起请求
        const { data: res } = await this.$http.post(
          "addtablemap",
          this.addForm
        );
        if (res != "Success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getTableMap();
      });
    },
    async deleteTableMap(id) {
      const confirmResult = await this.$confirm("确认是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.post("deletetablemap?id=" + id);
      if (res != "Success") {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getTableMap();
    },
    // 显示对话框
    async showUpdateDialog(id) {
      const { data: res } = await this.$http.get("getupdatetablemap?id=" + id);
      console.log("res=" + res);
      this.updateForm = res;
      this.updateDialogVisible = true;
    },
    // 关闭对话框
    updateDialogClose() {
      // 重置信息
      this.$refs.updateFormRef.resetFields();
    },
    // 确认修改
    updateTableMap() {
      this.$refs.updateFormRef.validate(async (valid) => {
        if (!valid) return;
        //发起修改请求
        const { data: res } = await this.$http.post(
          "updatetablemap",
          this.updateForm
        );
        console.log(res);
        if (res != "Success") return this.$message.err("修改失败");
        this.$message.success("修改成功");
        this.updateDialogVisible = false;
        this.getTableMap();
      });
    },
  },
};
</script>
<style lang="less">
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
</style>